<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div>
        <h3 class="title">
          <img
            class="title-container"
            :size="70"
            src="@/assets/logo.likede.png"
            alt=""
          />
        </h3>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.mobile" placeholder="请输入用户名" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="password"
          v-model="loginForm.password"
          :type="type"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="hadleLoginn"
        />
        <span class="svg-container">
          <svg-icon
            :icon-class="`${type === 'password' ? 'eye' : 'eye-open'}`"
            @click="changePwd"
          />
        </span>
      </el-form-item>

       <div class="tab">
        <el-form-item prop="code" class="validate">
          <span class="svg-container">
            <span class="el-icon-success" />
          </span>
          <el-input
            ref="code"
            v-model="loginForm.code"
            placeholder="请输入验证码"
          />
        </el-form-item>
        <el-image
          style="width: 130px; height: 50px"
          :src="photoUrl + loginForm.clientToken"
          fit="contain"
          class="img"
          @click="validateLogin()"
        />
      </div>

      <el-button
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin('loginForm')"
        >登录</el-button
      >

       <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>
    </el-form>
  </div>
</template>

<script>
import { validMobile } from "@/utils/validate";
import { imgSrc } from '@/utils/request';
export default {
  name: "Login",
  data() {
    // 自定义校验规则
    const checkPhone = (rule, value, callBcak) => {
      if (validMobile(value)) {
        callback(new Error('Please enter the correct user name'))
      
      } else {
        callBcak(new Error("用户名格式不正确"));
      }
    };
    return {
      loginForm: {  
        mobile: "admin",
        password: "111111",
      },
      loginRules: {
        mobile: [
          {
            required: true,
            message: "请填写用户名",
            trigger: "blur",
          },
          {
            validator: checkPhone,
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请填写密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 16,
            message: "密码格式不正确",
            trigger: "blur",
          },
        ],

        yanzhengma: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 16,
            message: "验证码格式不正确",
            trigger: "blur",
          },
        ],
      },
      type: "password",
      photoUrl:imgSrc
    };
  },
  watch: {},
  methods: {
    changePwd() {
      this.type = this.type === "password" ? "" : "password";
      this.$nextTick(() => {
        this.$refs.password.focus()
      });
    },
    submitLogin() {
      this.$refs.loginForm.validate((val) => {
        if (val) {
          console.log(1);
          this.$store.dispatch("user/loginAction", this.loginForm);
        }
      });
    },


  },
handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/loginAction', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }


};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #68b0fe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {

  .validate {
    width: 315px;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #999;
     background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url("~@/assets/likedetup.jpg");
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 10px 35px;
    margin: 280px auto 0;
    background-color: #fff;
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

.tab {
  display: flex;
}

  .title-container {
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -44px;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .loginBtn {
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg, #2e50e1, #6878f0);
    opacity: 0.91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
  }
}
</style>
